﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using XGameFrame;
using XGameFrame.Controls;

namespace XGameFrame
{
    /// <summary>
    /// Class to Handle Tiles
    /// </summary>
    public class TileHandler
    {
        #region Field Region
        private Texture2D tileSet;
        private Vector2 tileSize; 
        private Vector2 tileStep;
        private int xOffset;
        private int heightOffset;
        private Vector2 posiitionCorrection;
        private MapBase.MapType mapType;
        #endregion

        #region PropertyRegion
        /// <summary>
        /// Get the Texture of a TileSet or set it
        /// </summary>
        public Texture2D TileSet
        {
            get { return tileSet; }
            set { tileSet = value; }
        }

        /// <summary>
        /// Get size of one Tile in a TileSet or set it
        /// </summary>
        public Vector2 TileSize
        {
            get { return tileSize; }
            set { tileSize = value; }
        }

        /// <summary>
        /// Get size of the stepsize to the next tile or set it
        /// </summary>
        public Vector2 TileStep
        {
            get { return tileStep; }
            set { tileStep = value; }
        }

        /// <summary>
        /// Get size of push each odd row to side or set it
        /// </summary>
        public int XOffset
        {
            get { return xOffset; }
            set { xOffset = value; }
        }

        /// <summary>
        /// Get size of push each odd row from top
        /// </summary>
        public int HeightOffset
        {
            get { return heightOffset; }
            set { heightOffset = value; }
        }

        /// <summary>
        /// Get size of push each odd row to side or set it
        /// </summary>
        public Vector2 PosiitionCorrection
        {
            get { return posiitionCorrection; }
            set { posiitionCorrection = value; }
        }

        /// <summary>
        /// Get size of the stepsize to the next tile or set it
        /// </summary>
        public MapBase.MapType MapType
        {
            get { return mapType; }
            set { mapType = value; }
        }
        #endregion

        public TileHandler(Texture2D pTileSet,Vector2 pTileSize,MapBase.MapType pMapType)
        {
            MapType = pMapType;
            TileSet = pTileSet;
            TileSize = pTileSize;

            CalculateSpecificValues(pMapType);
        }

        /// <summary>
        /// Get the Rectangle of a specific Tile in a this TileSet
        /// </summary>
        /// <param name="pTileID">The specific ID of one tile</param>
        /// <returns>Rectangle</returns>
        public Rectangle GetSourceTileRectangle(int pTileID)
        {
            int tileSetCoordY = pTileID / (TileSet.Width / (int)TileSize.X);
            int tileSetCoordX = pTileID % (TileSet.Width / (int)TileSize.X);

            return new Rectangle(tileSetCoordX * (int)TileSize.X, tileSetCoordY * (int)TileSize.Y, (int)TileSize.X, (int)TileSize.Y);
        }

        #region Method Region

        private void CalculateSpecificValues(MapBase.MapType pMapType)
        {
            if(pMapType == MapBase.MapType.HexagonalMap)
            {
                int notTransparentCounterX = 0, notTransparentCounterY = 0;
                Color[] baseTileImageDataX = new Color[(int)TileSize.X];
                TileSet.GetData(0, new Rectangle(0, 0, (int)TileSize.X, 1), baseTileImageDataX, 0, baseTileImageDataX.Length);
                foreach(Color _colorPixel in baseTileImageDataX)
                {
                    if(_colorPixel.A == 255)
                        notTransparentCounterX++;
                }
                Color[] baseTileImageDataY = new Color[(int)TileSize.Y];
                TileSet.GetData(0, new Rectangle(0, 0, 1, (int)TileSize.Y), baseTileImageDataY, 0, baseTileImageDataY.Length);
                foreach(Color _colorPixel in baseTileImageDataY)
                {
                    if(_colorPixel.A == 255)
                        notTransparentCounterY++;
                }

                TileStep = new Vector2(notTransparentCounterX + TileSize.X, (TileSize.Y - notTransparentCounterY) / 2 + notTransparentCounterY);
                XOffset = notTransparentCounterX + (((int)TileSize.X - notTransparentCounterX) / 2);
                int tmpPosiitionCorrection = (-1) * (Math.Max(((int)TileSize.X - notTransparentCounterX) / 2, ((int)TileSize.Y - notTransparentCounterY) / 2)) - 1;
                PosiitionCorrection = new Vector2(tmpPosiitionCorrection,tmpPosiitionCorrection);
                HeightOffset =  0;
            }
            if(pMapType == MapBase.MapType.TileMap)
            {
                TileStep = TileSize;
                XOffset = HeightOffset = 0;
                PosiitionCorrection = new Vector2(0,0);
            }
            if(pMapType == MapBase.MapType.IsometricTileMap)
            {
                TileStep = new Vector2(TileSize.X, TileSize.Y / 4);
                XOffset = HeightOffset = (int)TileSize.X / 2;
                PosiitionCorrection = new Vector2((XOffset * (-1)), (TileSize.Y * (-1)));
            }
        }
        #endregion

    }
}
